import java.util.ArrayList;
import java.util.List;

public class BinaryTree {
    public static class TreeNode {
        TreeNode left;
        TreeNode right;
        char value;

        TreeNode(char value) {
            this.value = value;
        }
    }

    private TreeNode root;

    public void createBinaryTree() {
        TreeNode A = new TreeNode('A');
        TreeNode B = new TreeNode('B');
        TreeNode C = new TreeNode('C');
        TreeNode D = new TreeNode('D');
        TreeNode E = new TreeNode('E');
        TreeNode F = new TreeNode('F');
        TreeNode G = new TreeNode('G');
        TreeNode H = new TreeNode('H');
        this.root = A;
        A.left = B;
        A.right = C;
        B.left = D;
        B.right = E;
        C.left = F;
        C.right = G;
        E.right = H;
    }

    public void perOrder(TreeNode root) {
        if (root == null) {
            return;
        }
        System.out.println(root.value + " ");
        perOrder(root.left);
        perOrder(root.right);
    }

    public void inOrder(TreeNode root) {
        if (root == null) {
            return;
        }
        inOrder(root.left);
        System.out.println(root.value + " ");
        inOrder(root.right);
    }

    public void postOrder(TreeNode root) {
        if (root == null) {
            return;
        }
        postOrder(root.left);
        postOrder(root.right);
        System.out.println(root.value + " ");
    }

/*    public List<Integer> preorderTraversal(TreeNode root) {
        List<Integer> ret = new ArrayList<>();
        if(root==null){
            return ret;
        }
        ret.add(root.value);
        ret.addAll(preorderTraversal(root.left));
        ret.addAll(preorderTraversal(root.right));
        return ret;
    }*/


    // 获取树中节点的个数
    public int size(TreeNode root) {
        if (root == null) {
            return 0;
        }
        return size(root.right) + size(root.left) + 1;
    }

    public int getLeafNodeCount(TreeNode root){
        if (root == null) {
            return 0;
        }
        if(root.left==null&&root.right==null){
            return 1;
        }
        return getLeafNodeCount(root.left)+getLeafNodeCount(root.right);
    }

    // 获取第K层节点的个数
    public int getKLevelNodeCount(TreeNode root,int k){
        if (root == null) {
            return 0;
        }
        if(k==1){
            return 1;
        }
        return getKLevelNodeCount(root.right,k-1)
                +getKLevelNodeCount(root.left,k-1);
    }

    public int getHeight(TreeNode root){
        if (root == null) {
            return 0;
        }
        int hightleft=getHeight(root.left);
        int hightright=getHeight(root.right);
        return hightleft>hightright?(hightleft+1):(hightright+1);
    }
}

